package br.com.tiespec.web.Validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import br.com.tiespec.web.Util.MsgUtil;

public class ValidatorEmail implements Validator {

	@Override
	public void validate(FacesContext context, UIComponent toValidate, Object value)
			throws ValidatorException {
		String email = (String) value;
	    Pattern p = Pattern.compile("^[\\w-]+(\\.[\\w-]+)*@([\\w-]+\\.)+[a-zA-Z]{2,7}$");  
	    Matcher m = p.matcher(email);
	    if (!m.find()) {
	    	((UIInput)toValidate).setValid(false);
	    	FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
	    			MsgUtil.getMensagem("ValidateEmail"), "");
	    	context.addMessage(toValidate.getClientId(context), message); 
	    	return;
	    }
		
	}
	
}
